 using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace TimeWar
{
    public abstract class Weapon : VectorShape
    {
        //private int[] projectileCount = new int[3];

        /// <summary>
        /// Which player this projectile came from 
        /// </summary>
        private PlayerIndex player;

        /// <summary>
        /// The damage that this bullet does
        /// </summary>
        private int damage;

        private double endTime;

        private ProjectileType projectileType;

        private bool exploded = false;

        //private Particles particles;

        private bool projectileArmed;

        private Vector3 thrust;

        private double radius;

        private Cue weaponCue = null;

        #region Properties
        public int Damage
        {
            get
            {
                return damage;
            }
        }

        public PlayerIndex Player
        {
            get
            {
                return player;
            }
        }

        #endregion
        /// <summary>
        /// Cretes a new projectile
        /// </summary>
        /// <param name="game">Instance of the game</param>
        /// <param name="player">Which player it came from</param>
        /// <param name="position">The start position</param>
        /// <param name="velocity">The start velocity</param>
        /// <param name="angle">The direction its facing</param>
        /// <param name="time">The time the projectile was fired</param>
        public Weapon(Game game, PlayerIndex player, Vector3 position, Vector3 velocity, Vector3 direction, TimeSpan time, String pModelLocation)
            : base(game, ShapeTypes.Weapon)
        {
            this.player = player;
            Velocity = velocity;
            this.position = position;
            //shape = new Microsoft.Xna.Framework.Graphics.Model();
            Scale = 1f;

            ModelLocation = pModelLocation;
            setInitialValues();
            Initialize();
            //projectileCount[(int)player]++;

            //projectileType = (int)TimeWarGame.Players[(int)player].ProjectileType;
            projectileType = ProjectileType.DoubleMachineGun;

            //this.particles = particles;

            endTime = time.TotalSeconds + TimeWarGame.Settings.Weapons[(int)projectileType].Lifetime;
            thrust = Vector3.Multiply(Vector3.Normalize(velocity), TimeWarGame.Settings.Weapons[(int)projectileType].Acceleration);
            radius = 2;

            damage = TimeWarGame.Settings.Weapons[(int)projectileType].Damage;

            //shape = new BasicEffectShape(GameInstance, BasicEffectShapes.Projectile, projectileType, LightingType.InGame);

            //Evolved needs scaling
            ScaleX =
            ScaleY =
            ScaleZ = TimeWarGame.Settings.BulletScale;
            

            //rotation.X = MathHelper.ToRadians(90);
            //rotation.Y = 0;
            //rotation.Z = (float)angle;
            //rotation = direction;

            //Play 'shoot' sound
            switch (projectileType)
            {
                case ProjectileType.Peashooter:
                    weaponCue = Sound.Play(Sounds.PeashooterFire);
                    break;

                case ProjectileType.MachineGun:
                    weaponCue = Sound.Play(Sounds.MachineGunFire);
                    break;

                case ProjectileType.DoubleMachineGun:
                    weaponCue = Sound.Play(Sounds.DoubleMachineGunFire);
                    break;

                case ProjectileType.Rocket:
                    weaponCue = Sound.Play(Sounds.RocketExplode);
                    break;

                case ProjectileType.BFG:
                    weaponCue = Sound.Play(Sounds.BFGFire);
                    break;
            }
        }

        /// <summary>
        /// Updates the bullet. Removes it from scene when item is timed out
        /// </summary>
        /// <param name="time">Current game time</param>
        /// <param name="elapsedTime">Elapsed time since last update</param>
        public override void Update(TimeSpan time, TimeSpan elapsedTime)
        {
            Move(elapsedTime);

            //See if this bullets lifespan has expired
            if (time.TotalSeconds > endTime)
            {
                //BFG explodes and has a blast radius
                if (TimeWarGame.GameState == GameState.PlayEvolved && projectileType == ProjectileType.Rocket && !exploded)
                {
                    Sound.PlayCue(Sounds.Explosion);
                    //particles.AddExplosion(Position);

                    //We don't delete it this frame but we change the radius and the damage
                    exploded = true;
                    radius = 30;
                    damage = 3;
                }
                else
                {
                    DeleteProjectile();
                }
            }

            //acceleration = thrust;

            //For the rocket we need particles
            //if (projectileType == 3)
            //{
            //    particles.AddRocketTrail(shape.World, new Vector2(acceleration.X, -acceleration.Y));
            //}

            base.Update(time, elapsedTime);
        }

        public void DeleteProjectile()
        {
            //if (!delete)
            //    projectileCount[(int)player]--;
            //delete = true;
            Sound.Stop(weaponCue);
        }

        /// <summary>
        /// Checks if there is a collision between the this and the passed in item
        /// </summary>
        /// <param name="item">A scene item to check</param>
        /// <returns>True if there is a collision</returns>
        public bool Collide(SceneItem item)
        {
            // Until we get collision meshes sorted just do a simple sphere (well circle!) check
            float currentDistance = (Position - item.Position).Length();
            //bool colliding = base.Collide(item);

            // For projectiles, do not allow them to destroy the ship that just fired them!
            Ship shipItem = item as Ship;
            if ((shipItem != null) && (shipItem.Player == player))
            {

                //if (colliding && !projectileArmed)
                //{
                //    colliding = false;
                //}
                //else if (!colliding && !projectileArmed)
                //{
                //    // Once projectile is at least 2 ship radii away, it can arm!
                //    // This is becuase the bolt launches with your velocity at the time
                //    // and the ship can "catch up" to the bolt pretty easily if you are
                //    // thrusting in the direction you are firing.
                //    if (currentDistance > item.Radius * 2.0f)
                //        projectileArmed = true;
                //}


            }

            //return colliding;
            return true;

        }

        public override void onCollision()
        {
            Sound.Stop(weaponCue);
        }


    }
}